import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Comp {

    public static void main(String[] args) {
        class ListNode implements Comparable<ListNode> {
            int val;
            ListNode next;
            ListNode() {}
            ListNode(int val) { this.val = val; }
            ListNode(int val, ListNode next) {
                this.val = val; this.next = next;
            }

            @Override
            public int compareTo(ListNode node) {
                return this.val - node.val;
            }
        }

        ListNode[] list = new ListNode[5];
        for (int i = 5; i > 0; i--) {
            list[i-1] = new ListNode(5-i);
        }
        System.out.println("排序前");
        for (ListNode node : list) {
            System.out.println(node.val);
        }

        System.out.println(list[0].compareTo(list[1]));

        Arrays.sort(list);
        System.out.println("排序后");
        for (ListNode node : list) {
            System.out.println(node.val);
        }

        class ListNode2 {
            int val;
            ListNode2 next;
            ListNode2() {}
            ListNode2(int val) { this.val = val; }
            ListNode2(int val, ListNode2 next) {
                this.val = val; this.next = next;
            }
        }
        // 外部比较器
        Comparator<ListNode2> sorter1 = Comparator.comparing(node->node.val);
        ListNode2[] list2 = new ListNode2[5];
        for (int i = 5; i > 0; i--) {
            list2[i-1] = new ListNode2(5-i);
        }
        System.out.println("排序前");
        for (ListNode2 node : list2) {
            System.out.printf("%d\t",node.val);
        }

        Arrays.sort(list2, sorter1);
        System.out.println("排序后");
        for (ListNode2 node : list2) {
            System.out.printf("%d\t",node.val);
        }

    }
}
